package com.so206.config;

import com.so206.interceptr.BaseInterceptor;
import com.so206.interceptr.WebFireWallInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;

/**
 * 配置自定义拦截器
 */
@Configuration
@EnableWebMvc
public class WebAppConfigurer implements WebMvcConfigurer {

    @Autowired
    private BaseInterceptor baseInterceptor;

    @Autowired
    private WebFireWallInterceptor webFireWallInterceptor;

    private static final Logger logger = LoggerFactory.getLogger(WebAppConfigurer.class);

    public WebAppConfigurer() {
        logger.info("加载自定义拦截器...");
    }

    /**
     * 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        ArrayList<String> list = new ArrayList<>();
        list.add("/css/**");
        list.add("/font/**");
        list.add("/image/**");
        list.add("/file/**");
        list.add("/js/**");
        list.add("/*.txt");
        list.add("/*.ico");
        list.add("/*.html");
        list.add("/*.xml");
        list.add("/*.htm");
        list.add("/static/**");
        list.add("/kaptcha/**");
        registry.addInterceptor(webFireWallInterceptor).addPathPatterns("/**").excludePathPatterns("/clear_cache");
        registry.addInterceptor(baseInterceptor).addPathPatterns("/**").excludePathPatterns(list);
    }

    /**
     * 添加静态资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        ApplicationHome home = new ApplicationHome(getClass());
        File jarFile = home.getSource();
        String p = jarFile.getParentFile().toString();
        File dir = new File(p + "/WebRoot");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String path = dir.getAbsolutePath();
        registry.addResourceHandler("/**").addResourceLocations("file:" + path + "/");
        logger.info("映射静态资源路径:" + path);
        File file2 = new File(path + "/robots.txt");
        try {
            if (!file2.exists()) {
                FileOutputStream outputStream = new FileOutputStream(file2);
                outputStream.write("User-agent: * \nCrawl-delay: 15 \nDisallow: /admin/\n".getBytes());
                outputStream.flush();
                outputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
